package ru.fadeev.crammer.ui;

import javax.swing.text.*;


/**
 * Limited document extends PlainDocument, so it can contain limited number of
 * characters.
 * @author Konstantin Fadeyev
 *
 */
public class LimitedPlainDocument extends PlainDocument {
	private int limit;
	   
	public LimitedPlainDocument(int limit) {
		super();
		this.limit = limit;
    }
	  
	public void insertString(int offset, String  str, AttributeSet attr)
	       throws BadLocationException {
		if (str == null) return;

	    if ((getLength() + str.length()) <= limit) {
	    	super.insertString(offset, str, attr);
	    }
	}
}
